import axios from 'axios'
import { fromJS } from 'immutable'

// 获取 home 数据
export const CHANGE_HOME_DATA = 'CHANGE_HOME_DATA'
export const getHomeInfo = () => {
  return dispatch => {
    axios.get('/api/home.json').then(res => {
      const result = res.data.data
      const action = {
        type: CHANGE_HOME_DATA,
        topicList: result.topicList,
        articleList: result.articleList,
        recommendList: result.recommendList,
        writerList: result.writerList
      }
      dispatch(action)
    })
  }
}

// 获取更多 articleList 的数据
export const GET_MORE_ARTICLE_LIST = 'GET_MORE_ARTICLE_LIST'
export const getMoreArticle = page => {
  return dispatch => {
    axios.get('/api/homeMoreList.json?page=' + page).then(res => {
      const result = res.data.data
      const action = {
        type: GET_MORE_ARTICLE_LIST,
        list: fromJS(result),
        nextPage: page + 1
      }
      dispatch(action)
    })
  }
}

// 改变 showScroll 的值
export const CHANGE_SCROLL_SHOW = 'CHANGE_SCROLL_SHOW'
export const toggleTopShow = isShow => ({
  type: CHANGE_SCROLL_SHOW,
  isShow: isShow
})
